import unittest
import sys

sys.path.append('../')

from chemdb import dbHandle

class InsertCompoundTestCase(unittest.TestCase):
	def runTest(self):
		id = dbHandle.insertCompound("adenine", "C5H5N5", "7H-purin-6-amine")
		self.assert_(id, "id is null")
		self.assert_(id >= 0, "id is invalid")

	def tearDown(self):
		query = """DELETE FROM compound"""
		dbHandle.cursor.execute(query)
		dbHandle.db.commit()

class InsertBaseTestCase(unittest.TestCase):
	def setUp(self):
		dbHandle.insertCompound("adenine", "C5H5N5", "7H-purin-6-amine")
		self.id = dbHandle.getCompoundIdFromName('adenine')

	def tearDown(self):
		query = """DELETE FROM compound"""
		dbHandle.cursor.execute(query)
		dbHandle.db.commit()

class InsertInchiTestCase(InsertBaseTestCase):
	def runTest(self):
		self.assert_(dbHandle.insertInchi("1/C5H5N5/c6-4-3-5(9-1-7-3)10-2-8-4/h1-2H,(H3,6,7,8,9,10)/f/h7H,6H2", self.id), "no rows affected")

class InsertMolfileTestCase(InsertBaseTestCase):
	def runTest(self):
		self.assert_(dbHandle.insertMolfile("BLANK", self.id), "no rows affected")

class InsertFingerprintTestCase(InsertBaseTestCase):
	def runTest(self):
		self.assert_(dbHandle.insertFingerprint("213  zaf34   34 ", self.id), "no rows affected")

class InsertReferenceTestCase(unittest.TestCase):
	def runTest(self):
		id = dbHandle.insertReference("SYNTHESIS OF ADENINE FROM HCN TETRAMER AND AMMONIUM FORMATE", "LESLIE E. ORGEL", "2002", "Nature")
		self.assert_(id, "id is null")
		self.assert_(id >= 0, "id is invalid")

	def tearDown(self):
		query = """DELETE FROM reference"""
		dbHandle.cursor.execute(query)
		dbHandle.db.commit()

class InsertReactionTestCase(unittest.TestCase):
	def setUp(self):
		self.id = dbHandle.insertReference("SYNTHESIS OF ADENINE FROM HCN TETRAMER AND AMMONIUM FORMATE", "LESLIE E. ORGEL", "2002")

	def runTest(self):
		self.assert_(dbHandle.insertReaction(self.id, 110, 'water', 'liquid', 1.0, other='TEST'))

	def tearDown(self):
		query = """DELETE FROM reference"""
		dbHandle.cursor.execute(query)
		query = """DELETE FROM reaction"""
		dbHandle.cursor.execute(query)
		dbHandle.db.commit()

class InsertCatalystTestCase(InsertReactionTestCase):
	def setUp(self):
		refid = dbHandle.insertReference("SYNTHESIS OF ADENINE FROM HCN TETRAMER AND AMMONIUM FORMATE", "LESLIE E. ORGEL", "2002")
		self.reactionId = dbHandle.insertReaction(refid, 110, 'water', 'liquid', 1.0, other='TEST')

	def runTest(self):
		self.assert_(dbHandle.insertCatalyst("quartz", self.reactionId))

	def tearDown(self):
		InsertReactionTestCase.tearDown(self)
		query = """DELETE FROM catalyst"""
		dbHandle.cursor.execute(query)
		dbHandle.db.commit()

class InsertRelationTestCase(unittest.TestCase):
	def setUp(self):
		self.refId = dbHandle.insertReference("SYNTHESIS OF ADENINE FROM HCN TETRAMER AND AMMONIUM FORMATE", "LESLIE E. ORGEL", "2002")
		self.prodId = dbHandle.insertCompound("adenine", "C5H5N5", "7H-purin-6-amine")
		self.reactantId = dbHandle.insertCompound("formic acid", "CH5NO2", "azanium formate")
		self.reactionId = dbHandle.insertReaction(self.refId, 110, 'water', 'liquid', 1.0, other='TEST')
		

	def runTest(self):
		self.assert_(dbHandle.insertRelation(self.reactantId, self.prodId, self.reactionId))

	def tearDown(self):
		query = """DELETE FROM reference"""
		dbHandle.cursor.execute(query)
		query = """DELETE FROM reaction"""
		dbHandle.cursor.execute(query)
		query = """DELETE FROM compound"""
		dbHandle.cursor.execute(query)
		dbHandle.db.commit()
		

if __name__=="__main__":
	unittest.main()